/*
 * user.c
 *
 *  Created on: Nov 14, 2024
 *      Author: user
 */

#include "user.h"
#include "cJSON.h"
#include "sht2x_for_stm32_hal.h"

const static int Battery_Level_Percent_Table[11] =
{ 3000, 3650, 3700, 3740, 3760, 3795, 3840, 3910, 3980, 4070, 4150 };

uint8_t usart3_RecvBuff[USART3_RECVBUFF_SIZE];
char *JSON_CmdBuff[USART3_RECVBUFF_SIZE];
DevInfo device_info;

uint8_t is_BLE_configured = 0;

void User_Init()
{
    User_GenerateDevName(device_info.name);

    SHT2x_Init(&hi2c1);
    HAL_Delay(100);
    SHT2x_SetResolution(RES_14_12);
}

void User_GenerateDevName(char *name_buffer)
{
    char uid_1[8] =
    { 0 };
    uint32_t uid = HAL_GetUIDw0();

    memset(name_buffer, 0, 1);

    sprintf(uid_1, "%06X", uid);

    strcat(name_buffer, DEV_NAME_0"-");
    strcat(name_buffer, uid_1);
}

void parseJsonCmd(const char *jsonString, char *parsedCmd)
{
    static char failure_count = 0;
    static char last_str[250] = {0};

    if(failure_count>3)
    {
        memset(last_str, 0, sizeof(last_str));
    }

    strcat(last_str, jsonString);

    cJSON *root = cJSON_Parse(last_str);

    if (root == NULL)
    {
        failure_count++;
        return;
    }

    cJSON *cmdField = cJSON_GetObjectItemCaseSensitive(root, "CMD");

    if (cJSON_IsString(cmdField) && (cmdField->valuestring != NULL))
    {
        strcpy(parsedCmd, cmdField->valuestring);
    }

    failure_count = 0;
    memset(last_str, 0, sizeof(last_str));
    cJSON_Delete(root);
}

void parseCmd(const char *cmdString)
{
//	char cmdTemp[USART3_RECVBUFF_SIZE] = {0};
    strcat(cmdString, "\r\n");
    shellHandler_string(cmdString);
}

uint8_t User_BatVolToPowerPercent(uint16_t bat_vol)
{
    if (bat_vol < Battery_Level_Percent_Table[0])
    {
        return 0;
    }

    for (int i = 0; i < sizeof(Battery_Level_Percent_Table) / sizeof(Battery_Level_Percent_Table[0]); i++)
    {
        if (bat_vol < Battery_Level_Percent_Table[i])
        {
            return i * 10 - (10UL * (int) (Battery_Level_Percent_Table[i] - bat_vol)) / (int) (Battery_Level_Percent_Table[i] - Battery_Level_Percent_Table[i - 1]);
        }
    }

    return 100;
}

void User_RefreshData()
{
    device_info.temp_x10 = SHT2x_GetTemperature(1) * 10;
    device_info.humi_x10 = SHT2x_GetRelativeHumidity(1) * 10;

    device_info.volt1 = ADC_GetAverageData_UseDMA(0) * 0.80586; // 3300/4095 = 0.80586
    device_info.volt2 = ADC_GetAverageData_UseDMA(1) * 0.80586; // 3300/4095 = 0.80586
    device_info.volt3 = ADC_GetAverageData_UseDMA(2) * 0.80586; // 3300/4095 = 0.80586
    device_info.volt4 = ADC_GetAverageData_UseDMA(3) * 0.80586; // 3300/4095 = 0.80586
    device_info.volt5 = ADC_GetAverageData_UseDMA(4) * 0.80586; // 3300/4095 = 0.80586
    device_info.volt6 = ADC_GetAverageData_UseDMA(5) * 0.80586; // 3300/4095 = 0.80586
    device_info.volt7 = ADC_GetAverageData_UseDMA(6) * 0.80586; // 3300/4095 = 0.80586
    device_info.volt8 = ADC_GetAverageData_UseDMA(7) * 0.80586; // 3300/4095 = 0.80586
    device_info.volt9 = ADC_GetAverageData_UseDMA(8) * 0.80586; // 3300/4095 = 0.80586
    device_info.volt10 = ADC_GetAverageData_UseDMA(9) * 0.80586;    // 3300/4095 = 0.80586
    device_info.volt11 = ADC_GetAverageData_UseDMA(10) * 0.80586;   // 3300/4095 = 0.80586
    device_info.volt12 = ADC_GetAverageData_UseDMA(11) * 0.80586;   // 3300/4095 = 0.80586
    device_info.volt13 = ADC_GetAverageData_UseDMA(12) * 0.80586;   // 3300/4095 = 0.80586
    device_info.volt14 = ADC_GetAverageData_UseDMA(13) * 0.80586;   // 3300/4095 = 0.80586
    device_info.volt15 = ADC_GetAverageData_UseDMA(14) * 0.80586;   // 3300/4095 = 0.80586
    device_info.volt16 = ADC_GetAverageData_UseDMA(15) * 0.80586;   // 3300/4095 = 0.80586

    device_info.rest1 = device_info.volt1 * 100 / (3300 - device_info.volt1);  // kohm, Rsen = 100 kohm*Uout/(3300 mV - Uout)
    device_info.rest2 = device_info.volt2 * 100 / (3300 - device_info.volt2);
    device_info.rest3 = device_info.volt3 * 100 / (3300 - device_info.volt3);
    device_info.rest4 = device_info.volt4 * 100 / (3300 - device_info.volt4);
    device_info.rest5 = device_info.volt5 * 100 / (3300 - device_info.volt5);
    device_info.rest6 = device_info.volt6 * 100 / (3300 - device_info.volt6);
    device_info.rest7 = device_info.volt7 * 100 / (3300 - device_info.volt7);
    device_info.rest8 = device_info.volt8 * 100 / (3300 - device_info.volt8);
    device_info.rest9 = device_info.volt9 * 100 / (3300 - device_info.volt9);
    device_info.rest10 = device_info.volt10 * 100 / (3300 - device_info.volt10);
    device_info.rest11 = device_info.volt11 * 100 / (3300 - device_info.volt11);
    device_info.rest12 = device_info.volt12 * 100 / (3300 - device_info.volt12);
    device_info.rest13 = device_info.volt13 * 100 / (3300 - device_info.volt13);
    device_info.rest14 = device_info.volt14 * 100 / (3300 - device_info.volt14);
    device_info.rest15 = device_info.volt15 * 100 / (3300 - device_info.volt15);
    device_info.rest16 = device_info.volt16 * 100 / (3300 - device_info.volt16);

//        device_info.batVolt = (ADC_GetAverageData_UseDMA(2) * 0.80586 + 0) * 1.33333;   // V1.1
//        device_info.batLife = User_BatVolToPowerPercent(device_info.batVolt);
}

void User_SendDeviceStatus()
{
//    /****  Data Group - 1  ****/
//    cJSON *pRoot_board_1 = cJSON_CreateObject();
//    cJSON_AddStringToObject(pRoot_board_1, "ID", device_info.name);
//
//    cJSON_AddNumberToObject(pRoot_board_1, "BAT", device_info.batLife);
//    cJSON_AddNumberToObject(pRoot_board_1, "batvol", device_info.batVolt);
//    cJSON_AddNumberToObject(pRoot_board_1, "CEL", (float) device_info.temp_x10 / 10);
//    cJSON_AddNumberToObject(pRoot_board_1, "RH", (float) device_info.humi_x10 / 10);
//
//    char *sendData_1 = cJSON_Print(pRoot_board_1);
//    int data_len_1 = strlen(sendData_1);
//
//    HAL_UART_Transmit_DMA(&HAL_USART_USB, sendData_1, data_len_1);    // to USB port
//    HAL_UART_Transmit_DMA(&HAL_USART_BLE, sendData_1, data_len_1);    // to BLE module
//
//    vTaskDelay(100); // must delay for a while to avoid the release of the resources (sendData)
//
//    cJSON_free(sendData_1);
//    cJSON_Delete(pRoot_board_1); // 释放cJSON_CreateObject ()分配出来的内存空间
//
//    /****  Data Group - 2  ****/
//    cJSON *pRoot_board_2 = cJSON_CreateObject();
//    cJSON_AddStringToObject(pRoot_board_2, "ID", device_info.name);
//
//    cJSON_AddNumberToObject(pRoot_board_2, "vol1", device_info.volt1);
//    cJSON_AddNumberToObject(pRoot_board_2, "vol2", device_info.volt2);
//    cJSON_AddNumberToObject(pRoot_board_2, "vol3", device_info.volt3);
//    cJSON_AddNumberToObject(pRoot_board_2, "vol4", device_info.volt4);
//    cJSON_AddNumberToObject(pRoot_board_2, "vol5", device_info.volt5);
//    cJSON_AddNumberToObject(pRoot_board_2, "vol6", device_info.volt6);
//    cJSON_AddNumberToObject(pRoot_board_2, "vol7", device_info.volt7);
//    cJSON_AddNumberToObject(pRoot_board_2, "vol8", device_info.volt8);
//
//    char *sendData_2 = cJSON_Print(pRoot_board_2);
//    int data_len_2 = strlen(sendData_2);
//
//    HAL_UART_Transmit_DMA(&HAL_USART_USB, sendData_2, data_len_2);    // to USB port
//    HAL_UART_Transmit_DMA(&HAL_USART_BLE, sendData_2, data_len_2);    // to BLE module
//
//    vTaskDelay(100); // must delay for a while to avoid the release of the resources (sendData)
//
//    cJSON_free(sendData_2);
//    cJSON_Delete(pRoot_board_2); // 释放cJSON_CreateObject ()分配出来的内存空间
//
//    /****  Data Group - 3  ****/
//    cJSON *pRoot_board_3 = cJSON_CreateObject();
//    cJSON_AddStringToObject(pRoot_board_3, "ID", device_info.name);
//
//    cJSON_AddNumberToObject(pRoot_board_3, "vol9", device_info.volt9);
//    cJSON_AddNumberToObject(pRoot_board_3, "vol10", device_info.volt10);
//    cJSON_AddNumberToObject(pRoot_board_3, "vol11", device_info.volt11);
//    cJSON_AddNumberToObject(pRoot_board_3, "vol12", device_info.volt12);
//    cJSON_AddNumberToObject(pRoot_board_3, "vol13", device_info.volt13);
//    cJSON_AddNumberToObject(pRoot_board_3, "vol14", device_info.volt14);
//    cJSON_AddNumberToObject(pRoot_board_3, "vol15", device_info.volt15);
//    cJSON_AddNumberToObject(pRoot_board_3, "vol16", device_info.volt16);
//
//    char *sendData_3 = cJSON_Print(pRoot_board_3);
//    int data_len_3 = strlen(sendData_3);
//
//    HAL_UART_Transmit_DMA(&HAL_USART_USB, sendData_3, data_len_3);    // to USB port
//    HAL_UART_Transmit_DMA(&HAL_USART_BLE, sendData_3, data_len_3);    // to BLE module
//
//    vTaskDelay(100); // must delay for a while to avoid the release of the resources (sendData)
//
//    cJSON_free(sendData_3);
//    cJSON_Delete(pRoot_board_3); // 释放cJSON_CreateObject ()分配出来的内存空间

    /****  Data Group - 4  ****/
    cJSON *pRoot_board_4 = cJSON_CreateObject();
    cJSON_AddStringToObject(pRoot_board_4, "ID", device_info.name);

    cJSON_AddNumberToObject(pRoot_board_4, "ROW", 0);
    cJSON_AddNumberToObject(pRoot_board_4, "COL", 0);

    // Add an array to the object with key "VALS"
    cJSON *array = cJSON_AddArrayToObject(pRoot_board_4, "VALS");
    // Populate the array with some numbers
    cJSON_AddItemToArray(array, cJSON_CreateNumber(device_info.volt1));
    cJSON_AddItemToArray(array, cJSON_CreateNumber(device_info.volt2));
    cJSON_AddItemToArray(array, cJSON_CreateNumber(device_info.volt3));
    cJSON_AddItemToArray(array, cJSON_CreateNumber(device_info.volt4));
    cJSON_AddItemToArray(array, cJSON_CreateNumber(device_info.volt5));
    cJSON_AddItemToArray(array, cJSON_CreateNumber(device_info.volt6));
    cJSON_AddItemToArray(array, cJSON_CreateNumber(device_info.volt7));
    cJSON_AddItemToArray(array, cJSON_CreateNumber(device_info.volt8));
    cJSON_AddItemToArray(array, cJSON_CreateNumber(device_info.volt9));
    cJSON_AddItemToArray(array, cJSON_CreateNumber(device_info.volt10));
    cJSON_AddItemToArray(array, cJSON_CreateNumber(device_info.volt11));
    cJSON_AddItemToArray(array, cJSON_CreateNumber(device_info.volt12));
    cJSON_AddItemToArray(array, cJSON_CreateNumber(device_info.volt13));
    cJSON_AddItemToArray(array, cJSON_CreateNumber(device_info.volt14));
    cJSON_AddItemToArray(array, cJSON_CreateNumber(device_info.volt15));
    cJSON_AddItemToArray(array, cJSON_CreateNumber(device_info.volt16));

    char *sendData_4 = cJSON_Print(pRoot_board_4);
    int data_len_4 = strlen(sendData_4);

    HAL_UART_Transmit_DMA(&HAL_USART_USB, sendData_4, data_len_4);    // to USB port
    HAL_UART_Transmit_DMA(&HAL_USART_BLE, sendData_4, data_len_4);    // to BLE module

    vTaskDelay(80); // must delay for a while to avoid the release of the resources (sendData)

    cJSON_free(sendData_4);
    cJSON_Delete(pRoot_board_4); // 释放cJSON_CreateObject ()分配出来的内存空间
}
